// pages/dresses/dresses.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        time: '12:30',
        currentTab: 'all',
        viewMode: 'grid',
        searchKeyword: '',
        
        // 礼服数据
        dresses: [
            {
                id: 1,
                name: '法式蕾丝鱼尾婚纱',
                code: 'WD2401',
                color: '象牙白',
                price: '18,800',
                image: 'https://images.unsplash.com/photo-1594612658290-386d64b8dd1c?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80',
                statusText: '可用',
                statusClass: ''
            },
            {
                id: 2,
                name: '公主梦幻拖尾婚纱',
                code: 'WD1825',
                color: '纯白',
                price: '22,600',
                image: 'https://images.unsplash.com/photo-1585241920473-cb2a0ff38d8b?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80',
                statusText: '已预订',
                statusClass: 'unavailable'
            },
            {
                id: 3,
                name: '白色缎面修身礼服套装',
                code: 'SD0512',
                color: '纯白',
                price: '12,600',
                image: 'https://images.unsplash.com/photo-1596444382656-f81a489d9190?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80',
                statusText: '可用',
                statusClass: ''
            },
            {
                id: 4,
                name: '星光奢华晚礼服',
                code: 'ED0723',
                color: '香槟金',
                price: '15,800',
                image: 'https://images.unsplash.com/photo-1599387737494-d81825f5cd70?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80',
                statusText: '维护中',
                statusClass: 'maintenance'
            }
        ],
        
        // 最新入库礼服
        newDresses: [
            {
                id: 5,
                name: '轻奢浪漫纱裙',
                code: 'WD2403',
                color: '香槟色',
                price: '16,800',
                image: 'https://images.unsplash.com/photo-1562137369-1a1a0bc66744?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80',
                statusText: '可用',
                statusClass: '',
                tags: ['新款', '热门']
            },
            {
                id: 6,
                name: '经典黑色燕尾服套装',
                code: 'SD0615',
                color: '黑色',
                price: '8,600',
                image: 'https://images.unsplash.com/photo-1566195992011-5f6b21e539aa?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80',
                statusText: '可用',
                statusClass: '',
                tags: ['新款']
            }
        ],
        
        // 筛选后的礼服列表
        filteredDresses: [],
        filteredNewDresses: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 更新当前时间
        this.updateTime();
        this.getTimerID = setInterval(this.updateTime, 60000);
        
        // 如果有传入的筛选参数
        if (options.filter) {
            this.setData({
                currentTab: options.filter
            });
        }
        
        // 如果有传入的搜索关键词
        if (options.keyword) {
            this.setData({
                searchKeyword: options.keyword
            });
        }
        
        // 初始化筛选后的数据
        this.filterDresses();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearInterval(this.getTimerID);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    updateTime: function() {
        const date = new Date();
        const hours = date.getHours();
        const minutes = date.getMinutes();
        this.setData({
            time: `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`
        });
    },

    // 切换标签
    switchTab: function(e) {
        const tab = e.currentTarget.dataset.tab;
        this.setData({ currentTab: tab });
        
        // 根据标签筛选礼服
        this.filterDresses();
    },

    // 切换视图模式
    switchViewMode: function(e) {
        const mode = e.currentTarget.dataset.mode;
        this.setData({ viewMode: mode });
    },

    // 搜索输入
    onSearchInput: function(e) {
        this.setData({ searchKeyword: e.detail.value });
        
        // 根据关键词筛选礼服
        this.filterDresses();
    },

    // 筛选礼服
    filterDresses: function() {
        const { dresses, newDresses, currentTab, searchKeyword } = this.data;
        
        // 先根据标签筛选
        let filtered = [...dresses];
        let filteredNew = [...newDresses];
        
        if (currentTab !== 'all') {
            // 根据不同筛选条件筛选
            switch(currentTab) {
                case 'wedding':
                    filtered = filtered.filter(dress => dress.code.startsWith('WD'));
                    filteredNew = filteredNew.filter(dress => dress.code.startsWith('WD'));
                    break;
                case 'dress':
                    filtered = filtered.filter(dress => dress.code.startsWith('ED'));
                    filteredNew = filteredNew.filter(dress => dress.code.startsWith('ED'));
                    break;
                case 'suit':
                    filtered = filtered.filter(dress => dress.code.startsWith('SD'));
                    filteredNew = filteredNew.filter(dress => dress.code.startsWith('SD'));
                    break;
                case 'accessory':
                    filtered = filtered.filter(dress => dress.code.startsWith('AC'));
                    filteredNew = filteredNew.filter(dress => dress.code.startsWith('AC'));
                    break;
                case 'new':
                    filtered = [];
                    // 新款标签时，只显示新品区域的礼服
                    break;
            }
        }
        
        // 再根据搜索关键词筛选
        if (searchKeyword) {
            const keyword = searchKeyword.toLowerCase();
            filtered = filtered.filter(dress => 
                dress.name.toLowerCase().includes(keyword) || 
                dress.code.toLowerCase().includes(keyword) ||
                dress.color.toLowerCase().includes(keyword)
            );
            
            filteredNew = filteredNew.filter(dress => 
                dress.name.toLowerCase().includes(keyword) || 
                dress.code.toLowerCase().includes(keyword) ||
                dress.color.toLowerCase().includes(keyword)
            );
        }
        
        // 更新筛选后的数据
        this.setData({
            filteredDresses: filtered,
            filteredNewDresses: filteredNew
        });
    },

    // 查看礼服详情
    viewDressDetail: function(e) {
        const dressId = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/pages/dress_detail/dress_detail?id=${dressId}`
        });
    },
    
    // 查看礼服档期
    viewDressSchedule: function(e) {
        const dressCode = e.currentTarget.dataset.code;
        const dressName = e.currentTarget.dataset.name;
        wx.navigateTo({
            url: `/pages/dress_calendar/dress_calendar?dressCode=${dressCode}&dressName=${encodeURIComponent(dressName)}`
        });
    },

    // 显示添加选项
    showAddOptions: function() {
        wx.showActionSheet({
            itemList: ['添加婚纱', '添加礼服', '添加配饰', '导入礼服'],
            success: (res) => {
                // 暂时只提示功能开发中
                wx.showToast({
                    title: '功能开发中',
                    icon: 'none'
                });
            }
        });
    }
})